package com.ac.scum.log.task.task;


import com.ac.scum.player.score.bean.PlayerScoreUnlock;
import com.ac.scum.log.bean.PlayerUnlockScoreNotCounted;
import com.ac.scum.log.task.mapper.log.PlayerScoreUnlockMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor
public class UnlockScoreTask {

    private final RedisTemplate<String, Object> redisTemplate;
    private final PlayerScoreUnlockMapper playerScoreUnlockMapper;

    @Scheduled(cron = "0 */2 * * * ?")
    public void unlockScore() {
        Set<String> keys = redisTemplate.keys("unlock-score:*");
        if (keys == null || keys.isEmpty()) {
            return;
        }


        for (String key : keys) {
            List<Object> range = redisTemplate.opsForList().range(key, 0, -1);
            if (range == null || range.isEmpty()) {
                continue;
            }
            //获取到后删除
            redisTemplate.delete(key);
            List<PlayerUnlockScoreNotCounted> unlockScoreNotCountedList = range.stream().map(item -> (PlayerUnlockScoreNotCounted) item).toList();

            for (PlayerUnlockScoreNotCounted playerUnlockScoreNotCounted : unlockScoreNotCountedList) {
                PlayerScoreUnlock playerScoreUnlock = playerScoreUnlockMapper.selectOne(Wrappers.lambdaQuery(PlayerScoreUnlock.class)
                        .eq(PlayerScoreUnlock::getServerId, playerUnlockScoreNotCounted.getServerId())
                        .eq(PlayerScoreUnlock::getPlayerId, playerUnlockScoreNotCounted.getPlayerId())
                        .eq(PlayerScoreUnlock::getLockType, playerUnlockScoreNotCounted.getLockType())
                        .eq(PlayerScoreUnlock::getCountDate, LocalDate.now()));
                if (playerScoreUnlock == null) {
                    playerScoreUnlock = new PlayerScoreUnlock();
                    playerScoreUnlock.setCountDate(LocalDate.now());
                    playerScoreUnlock.setSteamId(playerUnlockScoreNotCounted.getSteamId());
                    playerScoreUnlock.setServerId(playerUnlockScoreNotCounted.getServerId());
                    playerScoreUnlock.setPlayerId(playerUnlockScoreNotCounted.getPlayerId());
                    playerScoreUnlock.setLockType(playerUnlockScoreNotCounted.getLockType());
                    playerScoreUnlock.setTotal(0);
                    playerScoreUnlock.setTotalScore(0D);
                    playerScoreUnlock.setTotalFailNum(0);
                    playerScoreUnlock.setTotalSuccess(0);
                    playerScoreUnlock.setTotalUseTime(0D);
                }
                playerScoreUnlock.setTotal(playerScoreUnlock.getTotal() + (playerUnlockScoreNotCounted.getFlag() ? 1 : 0) + playerUnlockScoreNotCounted.getFailNum());
                if (playerScoreUnlock.getTotal() == 0) {
                    continue;
                }
                playerScoreUnlock.setTotalUseTime(NumberUtils.toDouble(String.format("%.2f", (double) playerScoreUnlock.getTotalUseTime() + playerUnlockScoreNotCounted.getUseTime())));
                playerScoreUnlock.setTotalScore(NumberUtils.toDouble(String.format("%.2f", (double) playerScoreUnlock.getTotalScore() + playerUnlockScoreNotCounted.getScore())));
                playerScoreUnlock.setTotalSuccess(playerScoreUnlock.getTotalSuccess() + (playerUnlockScoreNotCounted.getFlag() ? 1 : 0));
                playerScoreUnlock.setTotalFailNum(playerScoreUnlock.getTotalFailNum() + playerUnlockScoreNotCounted.getFailNum());
                playerScoreUnlock.setAvgScore(NumberUtils.toDouble(String.format("%.2f", (double) playerScoreUnlock.getTotalScore() / playerScoreUnlock.getTotal())));
                playerScoreUnlock.setAvgUseTime(NumberUtils.toDouble(String.format("%.2f", (double) playerScoreUnlock.getTotalUseTime() / playerScoreUnlock.getTotal())));
                playerScoreUnlock.setAvgFailNum(NumberUtils.toDouble(String.format("%.2f", (double) playerScoreUnlock.getTotalFailNum() / playerScoreUnlock.getTotal())));

                playerScoreUnlock.setAvgSuccessNum(NumberUtils.toDouble(String.format("%.2f", (double) playerScoreUnlock.getTotalSuccess() / playerScoreUnlock.getTotal())));

                if (playerScoreUnlock.getId() == null) {
                    playerScoreUnlockMapper.insert(playerScoreUnlock);
                } else {
                    playerScoreUnlockMapper.updateById(playerScoreUnlock);
                }
            }
        }
        log.info("开锁数据统计完成");
    }

}
